package com.example.openguass.controller;


import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.Dish;
import com.example.openguass.entity.DishFlavor;
import com.example.openguass.service.DishService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.DishFlavorPageVO;
import com.example.openguass.vo.DishFlavorVO;
import com.example.openguass.vo.DishPageVO;
import com.example.openguass.vo.DishVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/dish")
@Api(value = "DishController", tags = "菜品接口")
public class DishController {
    private DishService dishService;
    @Autowired
    private void setDishService(DishService dishService){
        this.dishService = dishService;
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody DishVO dishVO){
        CommonResult result = new CommonResult().init();
        if (dishService.exit(dishVO)){
            return (CommonResult) result.failCustom(-10086,"当前菜品已存在");
        }
        if (dishService.insert(dishVO)){
            return result.success("创建菜品成功",dishVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"创建菜品失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) DishVO dishVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (dishService.getById(dishVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前菜品");
        }
        if (dishService.update(dishVO)){
            return result.success("DishVO",dishVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新菜品失败");
        }
    }

    @GetMapping("/delete/{dishId}")
    public CommonResult delete(@PathVariable String dishId){
        CommonResult result = new CommonResult().init();
        if (dishService.getById(dishId) == null){
            return (CommonResult) result.failCustom(-10086,"该菜品不存在");
        }
        if (dishService.removeById(dishId)){
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除菜品失败");
        }
    }

    @GetMapping("/select/{dishId}")
    public CommonResult select(@PathVariable String dishId){
        CommonResult result = new CommonResult().init();
        if (dishService.getById(dishId) == null){
            return (CommonResult) result.failCustom(-10086,"该菜品不存在");
        }
        Dish dish = dishService.getById(dishId);
        return result.success("dish",dish);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody DishPageVO dishPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        return (CommonResult) result.success("page",dishService.findPage(dishPageVO)).end();
    }
}
